package com.ruoyi.system.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.Activity;
import com.ruoyi.system.mapper.ActivityMapper;
import com.ruoyi.system.mapper.ImgResourceMapper;
import com.ruoyi.system.service.IActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 研学活动Service业务层处理
 *
 * @author xin
 * @date 2024-04-19
 */
@Service
public class ActivityServiceImpl implements IActivityService {
    @Autowired
    private ActivityMapper activityMapper;
    @Autowired
    private ImgResourceMapper imgResourceMapper;

    /**
     * 查询研学活动
     *
     * @param id 研学活动主键
     * @return 研学活动
     */
    @Override
    public Activity selectActivityById(Long id) {
        return activityMapper.selectActivityById(id);
    }

    /**
     * 查询研学活动列表
     *
     * @param activity 研学活动
     * @return 研学活动
     */
    @Override
    public List<Activity> selectActivityList(Activity activity)
    {
        return activityMapper.selectActivityList(activity);
    }

    /**
     * 新增研学活动
     *
     * @param activity 研学活动
     * @return 结果
     */
    @Override
    public int insertActivity(Activity activity) {
        activity.setCreateTime(DateUtils.getNowDate());
        int i = activityMapper.insertActivity(activity);

        String path = "http://156.238.238.108:8080";
        Long activityId = activity.getId();
        int sort = 1;

        String activityImagePath = activity.getActivityImagePath();

        if (activityImagePath != null) {
            String[] activityImagePathSp = activityImagePath.split(",");

            // 插入新的banner图片
            if (activityImagePathSp.length > 0) {
                for (String s : activityImagePathSp) {
                    String src = path + s;
                    System.out.println(s);
                    System.out.println(src);
                    imgResourceMapper.insertImgResource(src, "activity_banner", sort, activityId);
                    sort++;
                }
            }
        }
        sort = 1;
        String activityDetailImagePath = activity.getActivityDetailImagePath();
        if (activityDetailImagePath != null) {
            String[] activityDetailImagePathSp = activityDetailImagePath.split(",");

            // 插入新的banner图片
            if (activityDetailImagePathSp.length > 0) {
                for (String s : activityDetailImagePathSp) {
                    String src = path + s;
                    imgResourceMapper.insertImgResource(src, "activity_detail", sort, activityId);
                    sort++;
                }
            }
        }
        activity.setCreateTime(DateUtils.getNowDate());
        return i;
    }

    /**
     * 修改研学活动
     *
     * @param activity 研学活动
     * @return 结果
     */
    @Override
    public int updateActivity(Activity activity) {
        String path = "http://156.238.238.108:8080";
        Long activityId = activity.getId();
        int sort = 1;

        String activityImagePath = activity.getActivityImagePath();

        Map map = new HashMap<>();
        map.put("activityId", activityId);
        map.put("openType", "activity_banner");

        //清空banner图片
        imgResourceMapper.deleteImgResourceByActivityMap(map);
        if (activityImagePath != null) {
            String[] activityImagePathSp = activityImagePath.split(",");

            // 插入新的banner图片
            if (activityImagePathSp.length > 0) {
                for (String s : activityImagePathSp) {
                    String src = path + s;
//                System.out.println(s);
//                System.out.println(src);
                    imgResourceMapper.insertImgResource(src, "activity_banner", sort, activityId);
                    sort++;
                }
            }
        }

        String activityDetailImagePath = activity.getActivityDetailImagePath();
        sort = 1;
        //清空详情图片
        map.put("openType", "activity_detail");
        imgResourceMapper.deleteImgResourceByActivityMap(map);
        if (activityDetailImagePath != null) {
            String[] activityDetailImagePathSp = activityDetailImagePath.split(",");

            // 插入新的banner图片
            if (activityDetailImagePathSp.length > 0) {
                for (String s : activityDetailImagePathSp) {
                    String src = path + s;
                    imgResourceMapper.insertImgResource(src, "activity_detail", sort, activityId);
                    sort++;
                }
            }
        }
        activity.setUpdateTime(DateUtils.getNowDate());
        return activityMapper.updateActivity(activity);
    }

    /**
     * 批量删除研学活动
     *
     * @param ids 需要删除的研学活动主键
     * @return 结果
     */
    @Override
    public int deleteActivityByIds(Long[] ids)
    {
        return activityMapper.deleteActivityByIds(ids);
    }

    /**
     * 删除研学活动信息
     *
     * @param id 研学活动主键
     * @return 结果
     */
    @Override
    public int deleteActivityById(Long id)
    {
        return activityMapper.deleteActivityById(id);
    }
}
